package com.example.littlexs.rxjavademo;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;

import com.example.littlexs.rxjavademo.entity.HttpExceptionEntity;
import com.example.littlexs.rxjavademo.http.MyApi;
import com.example.littlexs.rxjavademo.http.MyMethods;
import com.example.littlexs.rxjavademo.http.SimpleMyCallBack;
import com.example.littlexs.rxjavademo.utils.ToastUtils;
import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.ResponseBody;
import retrofit2.adapter.rxjava.HttpException;
import rx.Subscriber;
import rx.subscriptions.CompositeSubscription;

/**
 * Created by Administrator on 2016/10/8.
 */
public class BaseActivity extends AppCompatActivity {
    /**
     * 页面布局的 根view
     */
    protected View mContentView;
    public CompositeSubscription mCompositeSubscription;
    public MyMethods myMethods;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void setContentView(int layoutResID) {
        View view = LayoutInflater.from(this).inflate(layoutResID, null);
        setContentView(view);
    }

    @Override
    public void setContentView(View view) {
        super.setContentView(view);
        mContentView = view;
    }


    public void initApi() {
        //创建 CompositeSubscription 对象 使用CompositeSubscription来持有所有的Subscriptions，然后在onDestroy()或者onDestroyView()里取消所有的订阅。
        mCompositeSubscription = new CompositeSubscription();
        // 构建 ApiWrapper 对象
        myMethods = new MyMethods();
    }

    public MyMethods getApiWrapper() {
        if (myMethods == null) {
            myMethods = new MyMethods();
        }
        return myMethods;
    }
    public CompositeSubscription getCompositeSubscription() {
        if (mCompositeSubscription == null) {
            mCompositeSubscription = new CompositeSubscription();
        }
        return mCompositeSubscription;
    }

    /**
     * 创建观察者  这里对观察着 过滤一次，过滤出我们想要的信息，错误的信息toast
     *
     * @param onNext
     * @param <T>
     * @return
     */
    protected <T> Subscriber newMySubscriber(final SimpleMyCallBack onNext) {
        return new Subscriber<T>() {
            @Override
            public void onCompleted() {
                onNext.onCompleted();
            }

            @Override
            public void onError(Throwable e) {
                if (e instanceof MyApi.APIException) {
                    MyApi.APIException exception = (MyApi.APIException) e;
                    ToastUtils.showToast(getApplicationContext(),exception.message);
                } else if (e instanceof HttpException) {
                    if (e instanceof HttpException) {
                        ResponseBody body = ((HttpException) e).response().errorBody();
                        Log.i("---",body.toString());
//                        try {
//                            String json = body.string();
//                            Gson gson = new Gson();
//                            HttpExceptionEntity mHttpExceptionBean = gson.fromJson(json, HttpExceptionEntity.class);
//                            if (mHttpExceptionBean != null && mHttpExceptionBean.getMessage() != null) {
//                                ToastUtils.showToast(getApplicationContext(),mHttpExceptionBean.getMessage());
//                                onNext.onError(mHttpExceptionBean);
//                            }
//                        } catch (IOException IOe) {
//                            IOe.printStackTrace();
//                        }
                    }
                }
            }

            @Override
            public void onNext(T t) {
                if (!mCompositeSubscription.isUnsubscribed()) {
                    onNext.onNext(t);
                }
            }
        };
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //一旦调用了 CompositeSubscription.unsubscribe()，这个CompositeSubscription对象就不可用了,
        // 如果还想使用CompositeSubscription，就必须在创建一个新的对象了。
        if(mCompositeSubscription != null){
            mCompositeSubscription.unsubscribe();
        }
    }
}
